{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import tensorflow as tf\n",
    "from tensorflow.keras.layers import LSTM\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# LSTM层的参数 - return_sequences和return_state\n",
    "\n",
    "这两个参数都会影响LSTM层的输出。返回的内容不同。  \n",
    "![](https://windmissing.github.io/Bible-DeepLearning/assets/images/Chapter10/8.png)  \n",
    "`return_sequences`是控制如何返回图中的output,`return_state`是控制如何返回图中的state。  \n",
    "\n",
    "## return_sequences = False, return_state = False\n",
    "\n",
    "例：  \n",
    "输入数据为：`X.shape = (1,2,1)`,即1个样本，2个时间步，1个特征  \n",
    "输出数据为：`shape=(1, 1)`，即1个样本，在LSTM最后一个时间步的output，output向量是一维的"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: shape=(1, 1), dtype=float32, numpy=array([[-0.103333]], dtype=float32)>"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X = tf.constant([[[1],[2]]],dtype=tf.float32)\n",
    "layer = LSTM(1)\n",
    "layer(X)  "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## return_sequences = True, return_state = False\n",
    "\n",
    "例：  \n",
    "输入数据为：X.shape = (1,2,1),即1个样本，2个时间步，1个特征  \n",
    "输出数据为：shape=(1, 2, 1)，即1个样本，2个时间步，每个时间步输出一个output，output向量是一维的"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: shape=(1, 2, 1), dtype=float32, numpy=\n",
       "array([[[0.08441012],\n",
       "        [0.10217844]]], dtype=float32)>"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X = tf.constant([[[1],[2]]],dtype=tf.float32)\n",
    "layer = LSTM(1,return_sequences = True)\n",
    "layer(X)  "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## return_sequences = False, return_state = True\n",
    "\n",
    "例：  \n",
    "输入数据为：X.shape = (1,2,1),即1个样本，2个时间步，1个特征  \n",
    "输出数据有三个，依次为：  \n",
    "输出1：shape=(1, 1)，1个样本，在LSTM最后一个时间步的output，output向量是一维的  \n",
    "输出2：shape=(1, 1)，1个样本，在LSTM最后一个时间步的output，output向量是一维的  \n",
    "输出3：shape=(1, 1)，1个样本，在LSTM最后一个时间步的state，state向量是一维的"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<tf.Tensor: shape=(1, 1), dtype=float32, numpy=array([[0.07225075]], dtype=float32)>,\n",
       " <tf.Tensor: shape=(1, 1), dtype=float32, numpy=array([[0.07225075]], dtype=float32)>,\n",
       " <tf.Tensor: shape=(1, 1), dtype=float32, numpy=array([[0.16183122]], dtype=float32)>]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X = tf.constant([[[1],[2]]],dtype=tf.float32)\n",
    "layer = LSTM(1,return_state = True)\n",
    "layer(X)  "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## return_sequences = True, return_state = True\n",
    "\n",
    "例：  \n",
    "输入数据为：X.shape = (1,2,1),即1个样本，2个时间步，1个特征  \n",
    "输出数据有三个，依次为：  \n",
    "输出1：shape=(1, 2, 1)，即1个样本，2个时间步，每个时间步输出一个output，output向量是一维的   \n",
    "输出2：shape=(1, 1)，即1个样本，在LSTM最后一个时间步的output，output向量是一维的  \n",
    "输出3：shape=(1, 1)，即1个样本，在LSTM最后一个时间步的state，state向量是一维的"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<tf.Tensor: shape=(1, 2, 1), dtype=float32, numpy=\n",
       " array([[[0.12106458],\n",
       "         [0.21484499]]], dtype=float32)>,\n",
       " <tf.Tensor: shape=(1, 1), dtype=float32, numpy=array([[0.21484499]], dtype=float32)>,\n",
       " <tf.Tensor: shape=(1, 1), dtype=float32, numpy=array([[0.43435505]], dtype=float32)>]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X = tf.constant([[[1],[2]]],dtype=tf.float32)\n",
    "layer = LSTM(1, return_sequences = True, return_state = True)\n",
    "layer(X)  "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
